<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/static/js/jquery-3.7.1.min.js"></script>
    <style>
        table {
            margin-top: 40px;
            width: 100%;
            border-collapse: collapse;
            border: 2px solid #333;
        }

        th,
        td {
            padding: 10px;
            text-align: center;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f2f2f2;
            color: #333;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        tr:nth-child(odd) {
            background-color: #a2cb89;
        }

        tbody tr:nth-child(odd) {
            background-color: #d9d9d9;
        }

        table thead tr td {
            font: 1.5em sans-serif;
        }

        table caption {
            font: 2em sans-serif;
        }
    </style>
</head>

<body>
    <table>
        <caption>海南经贸职业技术学院媒体新闻表</caption>
        <thead>
            <tr>
                <td>序号</td>
                <td>日期</td>
                <td>媒体</td>
                <td>标题</td>
            </tr>
        </thead>
        <tbody id="students">
        </tbody>
    </table>
    <script>
        let newsList = new Array();
        $.ajax({
            url: "/proxy?url=" + 'https://www.hceb.edu.cn/xwzx/mtjm.htm',
            contentType: "application/json",
            success: function (data) {
                newsList.push($(data).find('div.m_new1 li'));


                let totolNews = $(data).find('span.p_t').text().match(/\d+/)[0];
                let totolPages = Math.ceil(totolNews / 20);
                getData(totolPages);
            }
        });

        function getData(totolPages) {
            for (var i = totolPages - 1; i > 0; i--) {
                $.ajax({
                    url: "/proxy?url=" + 'https://www.hceb.edu.cn/xwzx/mtjm/' + i + '.htm',
                    contentType: "application/json",
                    success: function (data) {
                        newsList.push($(data).find('div.m_new1 li'));
                        if (newsList.length == totolPages) {
                            parseNewsList(newsList);
                        }
                    }
                });
            }
        }

        let medias = { 'opinion.people.com.cn': '人民网', 'cpc.people.com.cn': '人民日报', 'theory.people.com.cn': '中国共产党新闻网', 'politics.people.com.cn': '人民网' };
        function parseNewsList(newsList) {
            let counter = 1;
            for (var i = 0; i < newsList.length; i++) {
                newsList[i].each(function (index, element) {
                    // 这里可以访问当前的 li 元素
                    let cite = $(element).find('cite').text();
                    let link = $(element).find('a').attr('href');
                    let title = $(element).find('a').text();
                    let media = title;
                    try {
                        media = title.match(/【(.*?)】/)[1];
                        if (link.startsWith('.') || link.startsWith('/')) {
                            console.log(link);
                        } else {
                            let hostname = new URL(link).hostname;
                            medias[hostname] = media;
                        }

                    } catch (error) {
                        console.log(i, media);
                        if (link.startsWith('.') || link.startsWith('/')) {
                            console.log(link);
                        } else {
                            let hostname = new URL(link).hostname;
                            if (hostname in medias) {
                                media = medias[hostname]
                            }
                        }

                    }

                    $('#students').append(`
                        <tr>
                            <td>${counter}</td>
                            <td>${cite}</td>
                            <td>${media}</td>
                            <td><a href="${link}">${title}</a></td>
                        </tr>
                    `);
                    counter++;
                    console.log(medias);

                });
            }
        }
    </script>
</body>

</html>